module.exports = {
    defineReactive: function (obj, getFunc, setFunc) {
        // toast('开启数据代理')
        const defineReactiveProperty = (obj, key, val, getFunc, setFunc) => {
            Object.defineProperty(obj, key, {
                get() {
                    getFunc && getFunc(key);
                    return val;
                },
                set(newVal) {
                    val = newVal;
                    setFunc && setFunc(key, newVal);
                    // 可以在这里添加更多的响应式逻辑，比如通知监听器
                },
                enumerable: true,
                configurable: true
            });
        };

        const observe = (target, getFunc, setFunc) => {
            if (typeof target !== 'object' || target === null) {
                return;
            }

            for (let key in target) {
                defineReactiveProperty(target, key, target[key], getFunc, setFunc);
            }
        };

        observe(obj, getFunc, setFunc);
        return obj;
    },
    downloadFile: function () {
        console.log('下载文件');
        var timestamp = new Date().getTime();
        let url = 'https://qiaumi.isimi.net/files/20250114/6785d56b1dd85.png';
        let filePath = '/storage/emulated/0/DCIM/WeiXin/IMG_111222' + timestamp + '.png';
        let response = http.get(url);

        if (response.statusCode == 200) {
            let imageBytes = response.body.bytes();
            files.writeBytes(filePath, imageBytes);
            toast('下载成功' + filePath);
            media.scanFile(filePath);
            
            SendPost.filePath = filePath;
        } else {
            throw ('下载失败' + response.statusCode);
        }
    },
    getDate:function(){
        function formatDatetimeVal(str){
            var num;
            str>9?num=str:num="0"+str;
            
            return num;
        };
		var date = new Date();
		var year=date.getFullYear();//当前年份
		var month=date.getMonth();//当前月份
		var data=date.getDate();//天
		var hours=date.getHours();//小时
		var minute=date.getMinutes();//分
		var second=date.getSeconds();//秒
		
		var time = year+"-"+formatDatetimeVal((month+1))+"-"+formatDatetimeVal(data)+" "+formatDatetimeVal(hours)+":"+formatDatetimeVal(minute)+":"+formatDatetimeVal(second);
		
		return time;
	},
    percenValue(part){
        return (Math.random()).toFixed(1) <= (part/100)
    },
    getRandomArrayItem(arr) {
        const randomIndex = Math.floor(Math.random() * arr.length);
        return arr[randomIndex];
    }
}